import numpy as np

def calculate_energy(signal):
    return np.sum(signal ** 2)

def estimate_delays(signal, sensors, source, speed_of_sound=340.0):
    # 假设传感器和声源位置已知
    delays = []
    for sensor in sensors:
        distance = np.linalg.norm(np.array(sensor) - np.array(source))
        time_delay = distance / speed_of_sound
        delays.append(time_delay)
    return delays

def simulate_sensors(signal, source, sensors, sampling_rate):
    speed_of_sound = 340.0  # 声速，单位 m/s
    sensor_signals = []
    for sensor in sensors:
        distance = np.linalg.norm(np.array(sensor) - np.array(source))
        time_delay = int((distance / speed_of_sound) * sampling_rate)
        sensor_signal = np.roll(signal, time_delay)  # 模拟时延
        sensor_signals.append(sensor_signal)
    return sensor_signals

def estimate_source_position(sensors, delays, sampling_rate):
    # 使用简单的TDOA方法估计声源位置
    # 这里仅提供一个示例，需要根据具体问题修改
    # 假设所有传感器都在一条线上，声源也在这条线上
    # 计算声源到每个传感器的距离
    distances = [delay * 340.0 for delay in delays]  # 声速340 m/s
    # 这里可以使用更复杂的定位算法
    return np.mean(distances)  # 简化处理，仅为示例

# 假设
signal = np.random.normal(0, 1, 1000)
sampling_rate = 1000  # Hz
source = [0, 0]  # 声源位置
sensors = [[-5, 0], [5, 0]]  # 传感器位置
sensor_signals = simulate_sensors(signal, source, sensors, sampling_rate)
delays = estimate_delays (s, sampling_rate) for s in sensor_signals
position_estimate = estimate_source_position(sensors, delays, sampling_rate)

print("估计的声源位置:", position_estimate)
# 估计的声源位置: 0.0